package com.bsoft.hospitalchs.ui.reservation;

import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.base.BaseFragment;
import com.bsoft.hospitalchs.common.MyApplication;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.bsoft.hospitalchs.ui.main.MainActivity;
import com.bsoft.hospitalchs.utils.LogUtil;
import com.bsoft.hospitalchs.utils.SpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import butterknife.Bind;

/**
 * Created by 泅渡者
 * Created on 2017/6/27.
 */

public class ReservationFragment extends BaseFragment<ReservationContract.Presenter> implements ReservationContract.View {


    @Bind(R.id.webview)
    WebView webview;
    WebSettings mWebSettings;
    SearchResBean searchResBean;
    String userNme = "";
    ProgressBar progressBar;
    @Override
    public int onSetLayoutId() {
        return R.layout.activity_reservation;
    }

    @Override
    public void initView() {
        this.createPresenter(new ReservationPresenter(this, getActivity()));
        progressBar = (ProgressBar) mContentView.findViewById(R.id.progressBar);
        Gson gson = new Gson();
        searchResBean = gson.fromJson(getActivity().getIntent().getStringExtra("SearchResBean").toString(), new TypeToken<SearchResBean>() {
        }.getType());
        userNme = (String) SpUtils.get(MyApplication.getInstance(),"UserName","");
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);//取消滚动条

        // 启用javascript
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBlockNetworkImage(false);
        webview.getSettings().setSupportMultipleWindows(false);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setBuiltInZoomControls(false);

        webview.getSettings().setDefaultTextEncodingName("utf-8");
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        //设置WebChromeClient类
        webview.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                System.out.println("标题在这里" + title);
//                tvTitle.setText(title);
            }

            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
//                    String progress = "正在加载：" + newProgress + "%";
                    progressBar.setProgress(newProgress);
                } else if (newProgress == 100) {
//                    String progress = newProgress + "%";
                    progressBar.setVisibility(View.GONE);

                }
            }
        });
        //设置不用系统浏览器打开,直接显示在当前Webview
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                view.loadUrl("file:///android_asset/error.html");
            }
        });

        LogUtil.e("emmpid",searchResBean.getBody().getEMPIID());
        String YiIP = (String) SpUtils.get(getActivity(), "YiIP", "219.234.5.196:8066");
        String Url ="http://"+YiIP+"/ywk/appointmentManager/gotoAppointmentDiag?patientHISID=" + searchResBean.getBody().getEMPIID()
                +"&idOrganization=330102356"+"&apiKey=574c38e955534262966f4617fc58641b"+"&userId="+userNme;
        webview.loadUrl(Url);

        Log.e("ReservationFragment", Url + "");

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingDialog() {

    }

}
